package com.tech.tool.controller;

import com.tech.tool.dataobject.DatabaseManage;
import com.tech.tool.dto.databaseManage.DatabaseManageDTO;
import com.tech.tool.service.DatabaseManageService;
import com.tech.tool.utils.ResultVOUtil;
import com.tech.tool.vo.databaseManage.DatabaseManageVO;
import com.tech.tool.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/database")
public class DatabaseController {

    @Autowired
    private DatabaseManageService databaseManageService;

    @PostMapping(path = "/list", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultVO list(@RequestBody DatabaseManageDTO databaseManageDTO) {

        List<DatabaseManage> databaseManageList = databaseManageService.findByDatabaseDeleteAndBuIdOrderByCreateTimeDesc(databaseManageDTO.getBuId());
        List<DatabaseManageVO> databaseManageVOList = new ArrayList<>();

        for (DatabaseManage databaseManage: databaseManageList) {
            DatabaseManageVO databaseManageVO = new DatabaseManageVO();
            BeanUtils.copyProperties(databaseManage, databaseManageVO);
            databaseManageVOList.add(databaseManageVO);
        }

        return ResultVOUtil.success(databaseManageVOList);
    }

}
